package com.ft.springboottravel.service.impl;

import com.ft.springboottravel.bean.Shop;
import com.ft.springboottravel.mapper.ShopMapper;
import com.ft.springboottravel.service.ShopService;
import com.ft.springboottravel.util.JsonUtils;
import com.ft.springboottravel.util.Pager;
import com.ft.springboottravel.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShopServiceImpl implements ShopService {
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private ShopMapper shopMapper;

    @Override
    public Pager<Shop> pager(Integer currentPage, String shop_name, String shop_tel, String shop_address) {
        Pager<Shop> pagers=new Pager<>();
        List<Shop> shops = shopMapper.SelectShopList();
        pagers.setList(shops);
        pagers.setPageSize(4);
        pagers.setCurrentPage(currentPage);
        Long shopTotal = shopMapper.getShopTotal(shop_name, shop_tel, shop_address);
        pagers.setTotal(shopTotal);
        return pagers;
    }

    @Override
    public long getShopTotal(String shop_name, String shop_tel, String shop_address) {
        return shopMapper.getShopTotal(shop_name,shop_tel,shop_address);
    }

    @Override
    public List<Shop> SelectShopList() {
        List<Shop> shops = shopMapper.SelectShopList();
        return shops;
    }

    @Override
    public Shop SelectShopId(Integer shop_id) {
        String s = redisUtils.get(shop_id.toString());
        if (s != null && s.length() > 0){
            Shop shop = JsonUtils.jsonToPojo(s, Shop.class);
            return shop;
        }else {
            Shop shop1 = shopMapper.SelectShopId(shop_id);
            if (shop1 !=null){
                String s1 = JsonUtils.objectToJson(shop1);
                redisUtils.set(shop_id.toString(),s1);
            }
            return shop1;
        }
    }

    @Override
    public void AddShop(Shop shop) {
        shopMapper.AddShop(shop);
    }

    @Override
    public void DelShopById(Integer shop_id) {
        shopMapper.DelShopById(shop_id);
    }

    @Override
    public void EditShop(Shop shop) {
        shopMapper.EditShop(shop);
    }
}
